package com.qf.mybatis;

import com.qf.mybatis.mapper.OrderMapper;
import com.qf.mybatis.mapper.ProductMapper;
import com.qf.mybatis.mapper.UserMapper;
import com.qf.mybatis.pojo.Order;
import com.qf.mybatis.pojo.Product;
import com.qf.mybatis.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

public class HelloMybatis2 {

    SqlSession sqlSession = null;
    @Before
    public void doBefore() throws IOException {
        //1.获得读取MyBatis配置文件的流对象
        InputStream is = Resources.getResourceAsStream("mybatis-config.xml");

        //2.构建SqlSession连接对象的工厂
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);

        //3.通过工厂获得连接对象
        sqlSession = factory.openSession();
    }
    @After
    public void doAfter(){
        sqlSession.commit();
        sqlSession.close();
    }

    @Test
    public void testDelete(){
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        int result = mapper.delete("ww");
        System.out.println(result);
//        sqlSession.commit();
    }
    @Test
    public void testUpdate(){
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = new User(3,"ww","11111","女",new Date(),new Date());
        int result = mapper.update(user);
        System.out.println(result);
        sqlSession.commit();
    }
    @Test
    public void testInsert(){
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = new User(null,"zl","22333","男",new Date(),null);
        int result=mapper.insert(user);
        System.out.println(user);
        sqlSession.commit();
    }
    @Test
    public void testInsert2(){
        ProductMapper mapper = sqlSession.getMapper(ProductMapper.class);
        Product product=new Product();
        product.setName("qq");
        int result=mapper.insertProduct(product);
        System.out.println(product);

    }
    @Test
    public void testInsert3(){
        //主键回填
        OrderMapper mapper =  sqlSession.getMapper(OrderMapper.class);
        Order order = new Order();
        order.setName("iphone13pros");
        int  result = mapper.insertOrder(order);

        System.out.println(order);

    }



}
